package savelog;

import java.util.ArrayList;
import java.util.List;

import com.dropbox.client2.DropboxAPI;
import com.dropbox.client2.DropboxAPI.Entry;
import com.dropbox.client2.exception.DropboxException;

import android.content.Context;
import android.os.AsyncTask;

/**
 * Title: ListDeviceDir class for CSIS 343 Final Project 
 * Description: CS 343 Final Project - ListDeviceDir This class will list all the directories in Dropbox that are not for this current device 
 * the list of games and the information about a game
 * 
 * @author Erik Olson
 * @email erik.olson-5@my.uwrf.edu
 * @author Mike Harmon
 * @email michael.harmon@my.uwrf.edu
 * @author Jack Friermuth
 * @email jack.friermuth@my.uwrf.edu
 * @author Luke Vang
 * @email luke.vang@my.uwrf.edu
 * @author Casey Golla
 * @email casey.golla@my.uwrf.edu
 * @date December 4th 2012
 */
public class ListDeviceDir extends AsyncTask<Void, Long, Boolean> {

	private DropboxAPI<?> mApi;

	private ArrayList<String> DEVICE_DIRECTORIES;
	private final String DEVICE_NAME;

	/**
	 * Method to take in shared Preferences and save it to the Dropbox directory
	 * @param context
	 * @param api
	 * @param dname
	 */
	public ListDeviceDir(Context context, DropboxAPI<?> api, String dname) {
		DEVICE_DIRECTORIES = new ArrayList<String>();
		mApi = api;

		DEVICE_NAME = dname;
	}

	/**
	 * Method to check folder contents and save them into an array
	 * @param array
	 * @return array
	 * @Override
	 */
	protected Boolean doInBackground(Void... params) {
		try {
			Entry dirent = mApi.metadata("/", 0, null, true, null);
			List<Entry> Folders = dirent.contents;
			for (Entry entry : Folders) {
				if (!entry.fileName().equals(DEVICE_NAME)) {
					DEVICE_DIRECTORIES.add("/" + entry.fileName() + "/");
				}
			}
		} catch (DropboxException e) {
			e.printStackTrace();
		}

		return null;
	}

	/**
	 * Method to return an arraylist of the directories
	 * @return DEVICE_DIRECTORIES
	 */
	public ArrayList<String> getDeviceDir() {
		return DEVICE_DIRECTORIES;
	}

}
